<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身高" prop="userHeight">
        <el-input
          v-model="queryParams.userHeight"
          placeholder="请输入身高"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体重" prop="userWeight">
        <el-input
          v-model="queryParams.userWeight"
          placeholder="请输入体重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血压" prop="bloodPressure">
        <el-input
          v-model="queryParams.bloodPressure"
          placeholder="请输入血压"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="心率" prop="heartRate">
        <el-input
          v-model="queryParams.heartRate"
          placeholder="请输入心率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血常规" prop="bloodRoutine">
        <el-input
          v-model="queryParams.bloodRoutine"
          placeholder="请输入血常规"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="尿常规" prop="urinalysis">
        <el-input
          v-model="queryParams.urinalysis"
          placeholder="请输入尿常规"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="肝功能" prop="liverFunction">
        <el-input
          v-model="queryParams.liverFunction"
          placeholder="请输入肝功能"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="肾功能" prop="renalFunction">
        <el-input
          v-model="queryParams.renalFunction"
          placeholder="请输入肾功能"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血脂" prop="bloodFat">
        <el-input
          v-model="queryParams.bloodFat"
          placeholder="请输入血脂"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血糖" prop="bloodSugar">
        <el-input
          v-model="queryParams.bloodSugar"
          placeholder="请输入血糖"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="眼科" prop="ophthalmology">
        <el-input
          v-model="queryParams.ophthalmology"
          placeholder="请输入眼科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="耳鼻喉科" prop="otolaryngology">
        <el-input
          v-model="queryParams.otolaryngology"
          placeholder="请输入耳鼻喉科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="口腔科" prop="departmentOfStomatology">
        <el-input
          v-model="queryParams.departmentOfStomatology"
          placeholder="请输入口腔科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="甲状腺" prop="thyroid">
        <el-input
          v-model="queryParams.thyroid"
          placeholder="请输入甲状腺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:message:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:message:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:message:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:message:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="用户绑定id" align="center" prop="userId" />
      <el-table-column label="身高" align="center" prop="userHeight" />
      <el-table-column label="体重" align="center" prop="userWeight" />
      <el-table-column label="血压" align="center" prop="bloodPressure" />
      <el-table-column label="心率" align="center" prop="heartRate" />
      <el-table-column label="血常规" align="center" prop="bloodRoutine" />
      <el-table-column label="尿常规" align="center" prop="urinalysis" />
      <el-table-column label="肝功能" align="center" prop="liverFunction" />
      <el-table-column label="肾功能" align="center" prop="renalFunction" />
      <el-table-column label="血脂" align="center" prop="bloodFat" />
      <el-table-column label="血糖" align="center" prop="bloodSugar" />
      <el-table-column label="眼科" align="center" prop="ophthalmology" />
      <el-table-column label="耳鼻喉科" align="center" prop="otolaryngology" />
      <el-table-column label="口腔科" align="center" prop="departmentOfStomatology" />
      <el-table-column label="甲状腺" align="center" prop="thyroid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:message:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户体检信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="身高" prop="userHeight">
          <el-input v-model="form.userHeight" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="userWeight">
          <el-input v-model="form.userWeight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="血压" prop="bloodPressure">
          <el-input v-model="form.bloodPressure" placeholder="请输入血压" />
        </el-form-item>
        <el-form-item label="心率" prop="heartRate">
          <el-input v-model="form.heartRate" placeholder="请输入心率" />
        </el-form-item>
        <el-form-item label="血常规" prop="bloodRoutine">
          <el-input v-model="form.bloodRoutine" placeholder="请输入血常规" />
        </el-form-item>
        <el-form-item label="尿常规" prop="urinalysis">
          <el-input v-model="form.urinalysis" placeholder="请输入尿常规" />
        </el-form-item>
        <el-form-item label="肝功能" prop="liverFunction">
          <el-input v-model="form.liverFunction" placeholder="请输入肝功能" />
        </el-form-item>
        <el-form-item label="肾功能" prop="renalFunction">
          <el-input v-model="form.renalFunction" placeholder="请输入肾功能" />
        </el-form-item>
        <el-form-item label="血脂" prop="bloodFat">
          <el-input v-model="form.bloodFat" placeholder="请输入血脂" />
        </el-form-item>
        <el-form-item label="血糖" prop="bloodSugar">
          <el-input v-model="form.bloodSugar" placeholder="请输入血糖" />
        </el-form-item>
        <el-form-item label="眼科" prop="ophthalmology">
          <el-input v-model="form.ophthalmology" placeholder="请输入眼科" />
        </el-form-item>
        <el-form-item label="耳鼻喉科" prop="otolaryngology">
          <el-input v-model="form.otolaryngology" placeholder="请输入耳鼻喉科" />
        </el-form-item>
        <el-form-item label="口腔科" prop="departmentOfStomatology">
          <el-input v-model="form.departmentOfStomatology" placeholder="请输入口腔科" />
        </el-form-item>
        <el-form-item label="甲状腺" prop="thyroid">
          <el-input v-model="form.thyroid" placeholder="请输入甲状腺" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, getMessage, delMessage, addMessage, updateMessage } from "@/api/system/message";

export default {
  name: "Message",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户体检信息表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userHeight: null,
        userWeight: null,
        bloodPressure: null,
        heartRate: null,
        bloodRoutine: null,
        urinalysis: null,
        liverFunction: null,
        renalFunction: null,
        bloodFat: null,
        bloodSugar: null,
        ophthalmology: null,
        otolaryngology: null,
        departmentOfStomatology: null,
        thyroid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户绑定id不能为空", trigger: "change" }
        ],
        userHeight: [
          { required: true, message: "身高不能为空", trigger: "blur" }
        ],
        userWeight: [
          { required: true, message: "体重不能为空", trigger: "blur" }
        ],
        bloodPressure: [
          { required: true, message: "血压不能为空", trigger: "blur" }
        ],
        heartRate: [
          { required: true, message: "心率不能为空", trigger: "blur" }
        ],
        bloodRoutine: [
          { required: true, message: "血常规不能为空", trigger: "blur" }
        ],
        urinalysis: [
          { required: true, message: "尿常规不能为空", trigger: "blur" }
        ],
        liverFunction: [
          { required: true, message: "肝功能不能为空", trigger: "blur" }
        ],
        renalFunction: [
          { required: true, message: "肾功能不能为空", trigger: "blur" }
        ],
        bloodFat: [
          { required: true, message: "血脂不能为空", trigger: "blur" }
        ],
        bloodSugar: [
          { required: true, message: "血糖不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户体检信息列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userHeight: null,
        userWeight: null,
        bloodPressure: null,
        heartRate: null,
        bloodRoutine: null,
        urinalysis: null,
        liverFunction: null,
        renalFunction: null,
        bloodFat: null,
        bloodSugar: null,
        ophthalmology: null,
        otolaryngology: null,
        departmentOfStomatology: null,
        thyroid: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户体检信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户体检信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户体检信息编号为"' + ids + '"的数据项？').then(function() {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
